package com.example.nettypractice.server;

import com.example.nettypractice.info.TransportData;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;


@ChannelHandler.Sharable
public class FirstNettyServerHandler extends ChannelInboundHandlerAdapter {

    //处理所有接收到的数据，
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws IOException {

        System.out.println("server received " + msg);
        ctx.writeAndFlush("收到客户端对象实例");
    }

    //当前数据的读取的最后一条消息，会进行调用，表示已完成最后的读取
    public void channelReadComplete(ChannelHandlerContext ctx){
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)
                .addListener(ChannelFutureListener.CLOSE);
    }

    //记录了异常并关闭了连接。
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
        cause.printStackTrace();
        ctx.close();
    }
}
